package crons

import (
	"context"
	"github.com/gogf/gf/v2/os/gtime"
	"hotgo/internal/dao"
	"hotgo/internal/library/cron"
	"hotgo/internal/model/entity"
	"hotgo/internal/service"
)

func init() {
	cron.Register(MemberExpiry)
}

// MemberExpiry 会员到期检查任务（无参数）
var MemberExpiry = &cMemberExpiry{name: "member_expiry"}

type cMemberExpiry struct {
	name string
}

func (c *cMemberExpiry) GetName() string {
	return c.name
}

// Execute 执行任务
func (c *cMemberExpiry) Execute(ctx context.Context, parser *cron.Parser) (err error) {
	list := []entity.AdminMember{}
	err = service.AdminMember().Model(ctx).WhereLT(dao.AdminMember.Columns().ExpireTime, gtime.Now()).Scan(&list)
	if err != nil {
		parser.Logger.Warning(ctx, "会员到期检查任务-执行错误:%+v", err)
		return
	}
	for _, member := range list {
		if err = service.AdminMember().ResetMember(ctx, member.Id); err != nil {
			parser.Logger.Warning(ctx, "会员到期检查任务-执行错误:%+v", err)
			continue
		}
		parser.Logger.Infof(ctx, "会员到期检查任务-执行成功-会员ID:%v", member.Id)
	}
	return
}
